package com.niuxiaofa.newlaw.Network;

import android.widget.Toast;

import com.google.gson.Gson;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Bean.ByUserBean;
import com.niuxiaofa.newlaw.Bean.FollowFansBean;
import com.niuxiaofa.newlaw.Bean.VideosBean;
import com.niuxiaofa.newlaw.Tools.UIUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MePresenter {



    public static RequestBody setBody(Map map){
        Gson gson = new Gson();

        String json=gson.toJson(map);

        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);

    }



    public static void byUser(int pageNum ,interface_byUser interface_byUser) {

        Call<ByUserBean> call = ServiceManager.getInstance().creat(ApiService.class).byUser(pageNum,20);

        call.enqueue(new Callback<ByUserBean>() {
            @Override
            public void onResponse(Call<ByUserBean> call, Response<ByUserBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    ByUserBean baseBean = (ByUserBean) response.body();
                    interface_byUser.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<ByUserBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_byUser{
        void getResponse(ByUserBean u);
    }


    public static void community(Map map,interface_community interface_community) {

        Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).community(setBody(map));

        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    BaseBean baseBean = (BaseBean) response.body();
                    interface_community.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_community{
        void getResponse(BaseBean u);
    }






    public static void uploadVideo(Map map,interface_uploadVideo interface_uploadVideo) {

        Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).uploadVideo(setBody(map));

        call.enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    BaseBean baseBean = (BaseBean) response.body();
                    interface_uploadVideo.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_uploadVideo{
        void getResponse(BaseBean u);
    }

    public static void mineVideos(int currentIndex,interface_mineVideos interface_mineVideos) {

        Map map = new HashMap();
        map.put("current", currentIndex);
        map.put("size", 2);


        Call<VideosBean> call = ServiceManager.getInstance().creat(ApiService.class).mineVideos(setBody(map));

        call.enqueue(new Callback<VideosBean>() {
            @Override
            public void onResponse(Call<VideosBean> call, Response<VideosBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    VideosBean baseBean = (VideosBean) response.body();
                    interface_mineVideos.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<VideosBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_mineVideos{
        void getResponse(VideosBean u);
    }


    public static void followfans(Integer memberId,int type,int currtent,interface_followfans interface_followfans) {

        Map map=new HashMap();
        map.put("type",type);
        map.put("memberId",memberId);
        map.put("pageNum",currtent);
        map.put("pageSize",20);

        Call<FollowFansBean> call = ServiceManager.getInstance().creat(ApiService.class).followfans(setBody(map));

        call.enqueue(new Callback<FollowFansBean>() {
            @Override
            public void onResponse(Call<FollowFansBean> call, Response<FollowFansBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {
                    FollowFansBean baseBean = (FollowFansBean) response.body();
                    interface_followfans.getResponse(baseBean);
                }
            }

            @Override
            public void onFailure(Call<FollowFansBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(),"网络连接异常,请稍候在试",Toast.LENGTH_LONG).show();
            }
        });

    }
    public interface interface_followfans{
        void getResponse(FollowFansBean u);
    }

}
